/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package SteamEngineFX;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author Jeff Bendixsen <jeff@westcoastcode.com>
 * 
 * TODO: ROEMOVE THIS
 */
public class SEProperties {
    
    private static String UNITNAME = "UnitName";
    private static String URL = "url";
    private static String DRIVER = "driver";
    private static String USER = "user";
    private static String PASSWORD = "password";
    private static String DEFAULTED = "defaulted";
    
    
    private static String DEF_UNITNAME = "SteamEngineDBPU";
    private static String DEF_URL = "def-jdbc:derby://localhost:1527/SteamEngine";
    private static String DEF_DRIVER = "def-org.apache.derby.jdbc.ClientDriver";
    private static String DEF_USER = "def-se";
    private static String DEF_PASSWORD = "def-se";
    private static String DEF_DEFAULTED = "true";
    
    private static String PROPERTIES_FILE = "db.properties";
    
    public SEProperties(){
        
        Properties prop = new Properties();
        try{
            
            boolean defaulted = false;
            
            if (!prop.containsKey(URL)){
                prop.setProperty(URL, DEF_URL);
                defaulted = true;
            }
            if (!prop.containsKey(DRIVER)){
                prop.setProperty(DRIVER, DEF_DRIVER);
                defaulted = true;
            }
            if (!prop.containsKey(USER)){
                prop.setProperty(USER, DEF_USER);
                defaulted = true;
            }
            if (!prop.containsKey(PASSWORD)){
                prop.setProperty(PASSWORD, DEF_PASSWORD);
                defaulted = true;
            }
            if (!prop.containsKey(UNITNAME)){
                prop.setProperty(UNITNAME, DEF_UNITNAME);
                defaulted = true;
            }
            if (defaulted){
                prop.setProperty(DEFAULTED, DEF_DEFAULTED);
            }
            
            prop.store(new FileOutputStream(PROPERTIES_FILE), null);
            
            readFile();
            
        }catch(IOException ex){
            ex.printStackTrace();
        }
        
    }
    
    private String unitName;
    public String getUnitName(){
        return unitName;
    }
    
    private String url;
    public String getURL(){
        return url;
    }
    
    private String driver;
    public String getDriver(){
        return driver;
    }
    
    private String user;
    public String getUser(){
        return user;
    }
    
    private String password;
    public String getPassword(){
        return password;
    }
    
    public void readFile(){
        
        Properties prop = new Properties();
        url = "";
        driver = "";
        user = "";
        password = "";
        unitName = "";
        try{
            prop.load(new FileInputStream(PROPERTIES_FILE));

            if (prop.containsKey(URL)){
                url = prop.getProperty(URL);
            }
            if (prop.containsKey(DRIVER)){
                driver = prop.getProperty(DRIVER);
            }
            if (prop.containsKey(USER)){
                user = prop.getProperty(USER);
            }
            if (prop.containsKey(PASSWORD)){
                password = prop.getProperty(PASSWORD);
            }
            if (prop.containsKey(UNITNAME)){
                unitName = prop.getProperty(UNITNAME);
            }
            
        }catch(IOException ex){
            ex.printStackTrace();
        }
            
    }
    
    public void listProperties(){
        System.out.printf("Unitname: %s \n",getUnitName());
        System.out.printf("URL: %s \n",getURL());
        System.out.printf("Driver: %s \n",getDriver());
        System.out.printf("User: %s \n",getUser());
        System.out.printf("Password: %s \n",getPassword());
    }
    
}
